
#include "NodeAST.h"

int     NodeAST::_glnb = 1;

NodeAST::NodeAST() : _lol(NULL), _left(NULL), _right(NULL), _data(""), _nb(0)
{}

NodeAST::NodeAST(LOLCode& lol, std::string const & data, int nb) : _lol(&lol), _left(NULL), _right(NULL), _data(data), _nb(nb)
{}

void			NodeAST::nextAstToFile()
{
  std::string		s;

  for (int i = 0; i < this->getNb(); ++i)
    s += "\t";
  this->_lol->getFileCPP().writeLine(s + this->getData());
  if (this->_left != NULL)
    this->_left->nextAstToFile();
  if (this->_right != NULL)
    this->_right->nextAstToFile();
}

LOLCode const &		NodeAST::getLol() const
{
  return (*(this->_lol));
}

void			NodeAST::setLol(LOLCode & L)
{
  this->_lol = &L;
}

std::string const &	NodeAST::getData() const
{
  return (this->_data);
}

void			NodeAST::setData(std::string const & data)
{
  this->_data = data;
}

NodeAST *		NodeAST::getLeft() const
{
  return (this->_left);
}

int			NodeAST::getNb() const
{
  return (this->_nb);
}

NodeAST *		NodeAST::getRight() const
{
  return (this->_right);
}

void			NodeAST::nextAST()
{
  std::string		s;
  std::string		snew;

  s = this->_lol->getFileLOL().getLine();
  if (s != "")
    {
      if ((snew = this->_lol->getBnf().translate(s)) == "")
        throw new MyException (BNFERROR + s);
      if (this->_lol->getBnf().priority(s))
	{
	  this->_left = new NodeAST(*(this->_lol), snew, this->_glnb);
	  s = this->_lol->getFileLOL().getLine();
	  if (s != "")
	    {
	      if ((snew = this->_lol->getBnf().translate(s)) == "")
		throw new MyException (BNFERROR + s);
	      this->_right = new NodeAST(*(this->_lol), snew, this->_glnb);
	      if (s != "KTHX ")
		this->_right->nextAST();
	    }
	}
      else
	{
	  this->_right = new NodeAST(*(this->_lol), snew, this->_glnb);
	  if (s != "KTHX ")
	    this->_right->nextAST();
	}
    }
}

